/**
 * 
 */
package com.sc.softmgr.business.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.model.SoftmgrSoftinfo;
import com.acc.softmgr.model.SoftmgrSoftinfoExample;
import com.acc.softmgr.model.SysUser;
import com.alibaba.fastjson.JSONObject;
import com.sc.softmgr.business.service.ISoftmgrSoftinfoService;
import com.sc.softmgr.common.component.CacheObjectComponent;
import com.sc.softmgr.common.constant.DeleteStatus;
import com.sc.softmgr.common.constant.enums.RestStatus;
import com.sc.softmgr.common.utils.date.DateBase;
import com.sc.softmgr.common.web.DataGridObject;

/**
 * 
 * @content
 * 
 * @date 2017年5月8日
 * 
 * @author OprCalf
 * 
 */
@Controller
@RequestMapping("softmgrSoftinfo")
public class SoftmgrSoftinfoController {

	@Autowired
	private ISoftmgrSoftinfoService softmgrSoftinfoService;

	@Autowired
	private CacheObjectComponent cacheObjectComponent;

	@RequestMapping(value = "toSoftmgrSoftinfo", method = RequestMethod.GET)
	public String toSoftmgrSoftinfo() {
		return "softSoftinfo/softSoftinfo";
	}

	/**
	 * 查询列表页
	 * 
	 * @param project_name
	 * @param project_english
	 * @param page
	 * @param rows
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "searchSoftmgrSoftinfo", method = RequestMethod.GET)
	@ResponseBody
	public DataGridObject<SoftmgrSoftinfo> searchSoftmgrSoftinfo(SoftmgrSoftinfo model,
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows) throws Exception {
		DataGridObject<SoftmgrSoftinfo> responseObject = new DataGridObject<SoftmgrSoftinfo>();
		SoftmgrSoftinfoExample example = new SoftmgrSoftinfoExample();
		if (model != null && StringUtils.isNotEmpty(model.getSoft_name())) {
			example.getCriteria().andSoft_nameLike("%" + model.getSoft_name() + "%");
		}
		if (page == null || page == 0) {
			page = 1;
		}

		example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
		example.setCount(page * rows);
		example.setLimitStart((page - 1) * rows);

		List<SoftmgrSoftinfo> softmgrSoftinfos = softmgrSoftinfoService.selectByExample(example);
		for (SoftmgrSoftinfo softmgrSoftinfo : softmgrSoftinfos) {
			softmgrSoftinfo.setCategoryName(
					cacheObjectComponent.getSoftmgrCategory(softmgrSoftinfo.getCategory_id()).getCategory_name());
			softmgrSoftinfo.setSubSystemName(
					cacheObjectComponent.getSubSystem(softmgrSoftinfo.getSub_system_id()).getSub_system_name());
			if (softmgrSoftinfo.getCreatetime() != null) {
				softmgrSoftinfo.setCreateTimestring(
						DateBase.formatDate(softmgrSoftinfo.getCreatetime(), DateBase.DATE_PATTERN_DATETIME));
			}
		}
		responseObject.setTotal(page * rows);
		responseObject.setRows(softmgrSoftinfos);
		cacheObjectComponent.destroyCategoryMap();
		cacheObjectComponent.destroySubSystemMap();
		return responseObject;
	}

	/**
	 * 新增项目
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addSoftmgrSoftinfo", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject addSoftmgrSoftinfo(SoftmgrSoftinfo model, HttpServletRequest request)
			throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(model.getSoft_name()) || model.getCategory_id() == null
				|| model.getSub_system_id() == null) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		} else {
			// ----------判断唯一性------------
			SoftmgrSoftinfoExample example = new SoftmgrSoftinfoExample();
			example.getCriteria().andDeletedEqualTo(DeleteStatus.FALSE);
			example.getCriteria().andSoft_nameEqualTo(model.getSoft_name());
			List<SoftmgrSoftinfo> SoftmgrSoftinfos = softmgrSoftinfoService.selectByExample(example);
			if (SoftmgrSoftinfos != null && SoftmgrSoftinfos.size() > 0) {
				json.put("status", RestStatus.NOT_UNIQUE.code);
				json.put("message", RestStatus.NOT_UNIQUE.message);
				return json;
			} else {
				model.setCreatetime(new Date());
				model.setCreateby(currentUser.getUsername());
				model.setDeleted(DeleteStatus.FALSE);
				softmgrSoftinfoService.insert(model);
				json.put("status", RestStatus.SUCCESS.code);
				json.put("message", RestStatus.SUCCESS.message);
				return json;
			}
		}

	}

	/**
	 * 修改用户
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "editSoftmgrSoftinfo", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject editSoftmgrSoftinfo(SoftmgrSoftinfo model, HttpServletRequest request) throws Exception {
		JSONObject json = new JSONObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		if (StringUtils.isEmpty(model.getSoft_name()) || model.getCategory_id() == null
				|| model.getSub_system_id() == null) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		model.setDeleted(DeleteStatus.FALSE);
		model.setUpdateby(currentUser.getUsername());
		model.setUpdatetime(new Date());
		softmgrSoftinfoService.updateByPrimaryKey(model);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}

	/**
	 * 查询详情
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/{id}/searchSoftmgrSoftinfo", method = RequestMethod.GET)
	@ResponseBody
	public JSONObject searchUser(@PathVariable Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrSoftinfo softmgrSoftinfo = softmgrSoftinfoService.selectByPrimaryKey(id);
		softmgrSoftinfo.setHiddenCategoryId(String.valueOf(softmgrSoftinfo.getCategory_id()));
		softmgrSoftinfo.setHiddenSubSystemId(String.valueOf(softmgrSoftinfo.getSub_system_id()));
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		json.put("data", softmgrSoftinfo);
		return json;
	}

	/**
	 * 删除项目
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/removeSoftmgrSoftinfo", method = RequestMethod.POST)
	@ResponseBody
	public JSONObject removeSoftmgrSoftinfo(@RequestParam Integer id) throws Exception {
		JSONObject json = new JSONObject();
		if (id == null || id == 0) {
			json.put("status", RestStatus.PARAMETER_REQUIRED.code);
			json.put("message", RestStatus.PARAMETER_REQUIRED.message);
			return json;
		}
		SoftmgrSoftinfo SoftmgrSoftinfo = softmgrSoftinfoService.selectByPrimaryKey(id);
		SoftmgrSoftinfo.setDeleted(DeleteStatus.TRUE);
		softmgrSoftinfoService.updateByPrimaryKey(SoftmgrSoftinfo);
		json.put("status", RestStatus.SUCCESS.code);
		json.put("message", RestStatus.SUCCESS.message);
		return json;
	}
}
